<?php

class ViewOnceUrlGen_Validate_PasswordNotIdenticalToCurrent extends Zend_Validate_Abstract
{
	const PASSWORD_IDENTICAL = 'passwordIdentical';
	
	protected $_messageTemplates = array(
			self::PASSWORD_IDENTICAL => 'Password is the same as the current one'
	);
	
	public function isValid($value)
	{
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) {
			$userId = $auth->getIdentity()->id;
			$userDbTable = new Application_Model_DbTable_Users();
			$userData = $userDbTable->getUser($userId);
			$currentPassword = $userData['password'];
			
			if ($currentPassword === ViewOnceUrlGen_Util_Password::encryptPassword($value)) {
				$this->_error(self::PASSWORD_IDENTICAL);
				return false;
			}
		}
		
		return true;
	}
}